package edu.bupt.d1stack;

import java.util.Stack;

public class T05EvaluateReversePolishNOtation {
    public int evalRPN(String[] tokens) {
        if (tokens.length == 0){
            return 0;
        }
        Stack<Integer> integers = new Stack<Integer>();
        for (String str:tokens){
            if ("+".equals(str)){
                Integer pop = integers.pop();
                Integer pop1 = integers.pop();
                integers.push(pop + pop1);
            } else if ("-".equals(str)){
                Integer pop = integers.pop();
                Integer pop1 = integers.pop();
                integers.push(pop1 - pop);
            } else if ("*".equals(str)){
                Integer pop = integers.pop();
                Integer pop1 = integers.pop();
                integers.push(pop * pop1);
            }else if ("/".equals(str)){
                Integer pop = integers.pop();
                Integer pop1 = integers.pop();
                integers.push(pop1 / pop);
            }else {
                integers.push(Integer.parseInt(str));
            }
        }

        return integers.pop();
    }
}
